home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5101 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Suggestions welcome
  5. Date: Thu, 08 Feb 1996 14:25:47 +0200
  6. Organization: Carelcomp Forest
  7. Message-ID: <3119EBCB.776A@cmt.lpr.mail.carel.fi>
  8. References: <1996Feb6.225454.7550@venus.gov.bc.ca>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  14.  
  15. Steve Smith wrote:
  16. > I am writing a C++ Console application to extract information from a
  17. > proprietary database file.
  18. > In order to interpret some of the data in a record, I need to read in
  19. > a flat file which contains lists of values for certain variables. A
  20. > sample would be:
  21. > ++$ CITYNAMES
  22. > VICTORIA
  23. > VANCOUVER
  24. > EDMONTON
  25. > CALGARY
  26. > TORONTO
  27. > MONTREAL
  28. > QUEBEC
  29. > ++$ END
  30. > The city name field in the database record is a single byte, where 01 =
  31. > VICTORIA, 02 = VANCOUVER etc.
  32. > There are a number of such lists in the flat file, several have > 200 entries.
  33. > I want to read these lists in to my program, and be able to access them
  34. > using an array index (such as CityName[i]).
  35. > I don't know how many elements will be in the array from one run to the
  36. > next, so I can't (and wouldn't want to) statically allocate an array.
  37. > The only way I can think of to accomplish this, is the read through the
  38. > flat file twice.
  39. > On the first pass, count the number of elements required for each
  40. > unique list. Once I know how many elements are required I can dynamically
  41. > allocate an array of pointers to string for each list.
  42. > On the second pass through the file, I can allocate storage for each string,
  43. > copy it into memory and initialize the char* array with the pointer.
  44. > If anyone has any other suggestions on how I could accomplish this, I would be
  45. > interested in hearing from them (either e-mail or post).
  46. > Thanks in advance
  47.  
  48. I often use constructs such as this (this is just written offhand and not tested, but 
  49. you'll get the idea):
  50.  
  51.     typedef struct _things {
  52.         struct _things    *next;
  53.         char        *stuff_goes_here;
  54.     } THINGS;
  55.  
  56.     THINGS    *head = NULL;
  57.     THINGS    *tail = NULL;
  58.  
  59.     THINGS    *AddThing(void)
  60.     {
  61.         if (!tail)
  62.             tail = head = (THINGS *)calloc(1, sizeof(THINGS));
  63.         else
  64.             tail = tail->next = (THINGS *)calloc(1, sizeof(THINGS));
  65.         if (!tail) {
  66.             /* ERROR!! Out of memory */
  67.         }
  68.         return tail;
  69.     }
  70.  
  71.     ...
  72.  
  73.     /* add things */
  74.     THINGS    *tmp = AddThing();
  75.     tmp->stuff_goes_here = (char *)malloc(enough);
  76.     strcpy(tmp->stuff_goes_here, the_stuff);
  77.  
  78.     ...
  79.  
  80.     /* walk the list */
  81.     THINGS    *p;
  82.  
  83.     for (p = head; p != NULL; p = p->next) {
  84.         /* do something with p->stuff_goes_here */
  85.     }
  86.  
  87.     ...
  88.     /* clear the list */
  89.  
  90.     THINGS    *p, *q;
  91.  
  92.     for (p = head, q = NULL; p != NULL; p = q) {
  93.         q = p->next;
  94.         if (p->stuff_goes_here)
  95.             free(p->stuff_goes_here);
  96.         free(p);
  97.     }
  98.  
  99. HTH,
  100.  AriL
  101. -- 
  102. All my opinions are mine and mine alone.
  103.